<template>
  <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="434px">
    <el-form ref="formData" :model="formData" label-width="95px" :rules="rules">
      <el-form-item label="项目名称：" prop="name">
        <el-input
          v-model="formData.name"
          placeholder="请输入项目名称"
          :maxlength="50"
          class="show-word-limit"
          show-word-limit
        />
      </el-form-item>
      <el-form-item label="描述：" prop="description">
        <el-input
          v-model="formData.description"
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4 }"
          placeholder="请输入内容"
          :maxlength="255"
          class="show-word-limit"
          show-word-limit
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" style="display: block; text-align: center">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addProject, updateProject } from '@/api/project-management'
import defaultSettings from '@/settings'
import { generateUUID, postMessage, receivedMessage } from '@/utils/index'
export default {
  data() {
    return {
      dialogType: '',
      dialogTitle: '',
      dialogVisible: false,
      formData: {},
      rules: {
        name: [{ required: true, message: '请输入项目名称', trigger: 'blur' }]
      },
      addOrUpdateProjectRequestId: generateUUID()
    }
  },
  mounted() {
    receivedMessage(this.dataHandle)
  },
  methods: {
    dataHandle(result) {
      let self = this
      let e = defaultSettings.isWPF ? result : { data: JSON.parse(result) }
      if (e.data.webInfo.requestId === self.addOrUpdateProjectRequestId) {
        self.openMessage(e.data.response)
      }
    },
    is(type, row) {
      this.dialogType = type
      this.dialogTitle = type === 'add' ? '新建项目' : '编辑项目'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.formData.clearValidate()
      })
      this.formData = row
    },
    handleSubmit() {
      if (defaultSettings.isWPF || defaultSettings.isCEF) {
        this.addOrUpdateProjectRequestId = generateUUID()
        let message = {
          RequestUrl: process.env.VUE_APP_URL + '/project/' + (this.dialogType === 'add' ? 'add' : 'update'),
          RequestMethod: 'POST',
          Headers: {
            contentType: 'application/json'
          },
          QueryParameter: {},
          RequestBody: this.formData,
          WebInfo: {
            requestId: this.addOrUpdateProjectRequestId
          }
        }
        postMessage(message)
      } else {
        let operation = this.dialogType === 'add' ? addProject : updateProject
        operation(this.formData).then((res) => {
          this.openMessage(res)
        })
      }
    },
    openMessage(res) {
      if (res.success) {
        if (this.dialogType === 'add') {
          this.$emit('refreshData', 'add', res.data)
        } else {
          this.$emit('refreshData', 'edit', res.data)
        }

        this.dialogVisible = false
        this.$message.success(this.dialogType === 'add' ? '新建项目成功' : '编辑项目成功')
      } else {
        this.$message.error(res.message)
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
